<?php
class NewsController extends Zend_Controller_Action {
	protected $_baseUrl;
	protected $_contextPath;
	protected $_utilities;
	
	public function init() {
		$this->_baseUrl = $this->_request->getBaseURL ();
		$this->_contextPath = "news";
		
		$this->view->assign ( "baseUrl", $this->_baseUrl );
		$this->view->assign ( "contextPath", $this->_contextPath );
		
		$this->_utilities = new Utilities ( );
		$this->view->assign ( "utilities", $this->_utilities );
		
		$section = new SectionModel ( );
		$this->view->assign ( "sections", $section->fetchAll ( null, "sec_position ASC" ) );
		
		$commodity = new CommodityModel();
		
		$this->view->assign ( "commodities", $commodity->getVipCommodity(20) );

		$this->view->assign ( "hot_commodities", $commodity->getHotCommodity(20) );
	}
	
	public function errorAction() {
		$this->render ( "error" );
	}
	
	public function indexAction() {
		$articleDAO = new ArticleModel ( );
		$published = ( int ) 1;
		
		//Begin: render script for paging.
		$paging = new Paging ( );
		//Get item on page.
		//Get total page.
		//Get current page.
		$itemPerPage = Constant::ARTICLE_LIST_ITEM;
		$totalRecords = $articleDAO->getTotalRecords ( $published );
		$currentPages = ( int ) $this->_request->getParam ( 'page' );
		if ($currentPages == 0)
			$currentPages = 1;
			//Set url for paging.	
		//Render paging for page.	
		//Render paging title for page.	
		$url = $this->_baseUrl . "/" . $this->_contextPath ;
		$page = $paging->doPaging ( $totalRecords, $url, $currentPages, 0, 0, null, 'page', 'selected_page' );
		$page_title = $paging->doPageSeparator ( $totalRecords, $currentPages, $itemPerPage );
		//Set for view.
		$this->view->assign ( "page", $page );
		$this->view->assign ( "page_title", $page_title );
		//End: render script for paging.
		
		$listArticles = $articleDAO->getAllArticle ( $published, $currentPages, $itemPerPage );
		
		if ($currentPages > 1){
			$new_pages = $currentPages - 1;			
			$list_new = $articleDAO->getAllArticle ( $published, $new_pages, $itemPerPage );
			$this->view->assign("list_new", $list_new);
		}
		
		$old_pages = $currentPages + 1;
		$list_old = $articleDAO->getAllArticle ( $published, $old_pages, $itemPerPage );
		
		$this->view->assign ( "listArticles", $listArticles );
		$this->view->assign ( "currentPage", $currentPages );
		$this->view->assign ( "old_articles", $list_old );
		
		$this->render ( "list" );
	}
	
	public function viewAction() {
		$request = $this->getRequest ();
		$article_id = ( int ) $request->getParam ( 'article_id' );
		if ($article_id) {
			$articleDAO = new ArticleModel ( );
			$article = $articleDAO->getArticleById ( $article_id );
			
			// Kiem tra xem article co ton tai hay khong.
			if ($article) {
				$published = ( int ) 1;
				$paging = new Paging ( );
				$itemPerPage = $paging->itemPerPages;
				$currentPages = ( int ) $request->getParam ( 'page' );
				$listArticles = $articleDAO->getAllArticle ( $published, $currentPages, $itemPerPage );
				
				$old_pages = $currentPages + 1;
				$list_old = $articleDAO->getAllArticle ( $published, $old_pages, $itemPerPage + 1 );
				
				$this->view->assign ( "article", $article );
				$this->view->assign ( "articles", $listArticles );
				$this->view->assign ( "old_articles", $list_old );
				$this->view->assign ( "currentPage", $currentPages );
				
				$this->render ( "view" );
				return;
			}
		}
		
		$this->_redirect ( $this->_contextPath . "/error" );
	}

}//End class.